***************************************
* Analyzing survey data for RDD paper *
***************************************

* Loading data
**************

set more off
cd "/Users/damienbol/Dropbox/RDD france/Survey analysis/" // Set your own local path where you put dataset

use Provence_Idf_national_cleaned_data_with_scores.dta, clear

drop if ELEC_CON==.

* Separating two and three-candidates districts
***********************************************

gen ump_running=0
replace ump_running=1 if score_ump_2round!=.

gen soc_running=0
replace soc_running=1 if score_soc_2round!=.

gen vec_running=0
replace vec_running=1 if score_vec_2round!=.

gen dvd_running=0
replace dvd_running=1 if score_dvd_2round!=.

gen prv_running=0
replace prv_running=1 if score_prv_2round!=.

gen fg_running=0
replace fg_running=1 if score_fg_2round!=.

gen dvd1_running=0
replace dvd1_running=1 if score_dvd1_2round!=.

gen nce_running=0
replace nce_running=1 if score_nce_2round!=.

gen fn_running=0
replace fn_running=1 if score_fn_2round!=.

gen rdg_running=0
replace rdg_running=1 if score_rdg_2round!=.

gen dvg_running=0
replace dvg_running=1 if score_dvg_2round!=.

gen exd_running=0
replace exd_running=1 if score_exd_2round!=.

gen nbr_cand_running=ump_running+soc_running+vec_running+dvd_running+prv_running+fg_running+dvd1_running+nce_running+fn_running+rdg_running+dvg_running+exd_running

* Creating a turnout variable
*****************************

gen turnout=.
replace turnout=0 if PQ5_1==1
replace turnout=0 if PQ5_1==2
replace turnout=0 if PQ5_1==3
replace turnout=1 if PQ5_1==4
replace turnout=. if PQ5_1==9

replace turnout=1 if PQ5_2==1
replace turnout=0 if PQ5_2==2
replace turnout=. if PQ5_2==9


* Creating variables capturing first, second, and third scores in the district
******************************************************************************

gen score1=max(score_ump_1round, score_aut_1round, score_dvd_1round, score_cen_1round, score_exg_1round, score_aut1_1round, score_eco_1round, score_exg1_1round, score_soc_1round, score_vec_1round, score_dvg_1round, score_fg_1round, score_exg2_1round, score_fn_1round, score_dvd1_1round, score_exd_1round, score_dvd2_1round, score_dvd3_1round, score_prv_1round, score_nce_1round, score_eco1_1round, score_aut2_1round, score_exg3_1round, score_alli_1round, score_rdg_1round, score_eco2_1round, score_dvd4_1round, score_aut3_1round, score_aut4_1round, score_dvg1_1round, score_dvg2_1round, score_dvd5_1round, score_dvg3_1round, score_reg_1round, score_exd1_1round) if nbr_cand_running>1

gen score_ump_1round_2=score_ump_1round
replace score_ump_1round_2=0 if score_ump_1round==score1

gen score_aut_1round_2=score_aut_1round
replace score_aut_1round_2=0 if score_aut_1round==score1

gen score_dvd_1round_2=score_dvd_1round
replace score_dvd_1round_2=0 if score_dvd_1round==score1

gen score_cen_1round_2=score_cen_1round
replace score_cen_1round_2=0 if score_cen_1round==score1

gen score_exg_1round_2=score_exg_1round
replace score_exg_1round_2=0 if score_exg_1round==score1

gen score_aut1_1round_2=score_aut1_1round
replace score_aut1_1round_2=0 if score_aut1_1round==score1

gen score_eco_1round_2=score_eco_1round
replace score_eco_1round_2=0 if score_eco_1round==score1

gen score_exg1_1round_2=score_exg1_1round
replace score_exg1_1round_2=0 if score_exg1_1round==score1

gen score_soc_1round_2=score_soc_1round
replace score_soc_1round_2=0 if score_soc_1round==score1

gen score_vec_1round_2=score_vec_1round
replace score_vec_1round_2=0 if score_vec_1round==score1

gen score_dvg_1round_2=score_dvg_1round
replace score_dvg_1round_2=0 if score_dvg_1round==score1

gen score_fg_1round_2=score_fg_1round
replace score_fg_1round_2=0 if score_fg_1round==score1

gen score_exg2_1round_2=score_exg2_1round
replace score_exg2_1round_2=0 if score_exg2_1round==score1

gen score_fn_1round_2=score_fn_1round
replace score_fn_1round_2=0 if score_fn_1round==score1

gen score_dvd1_1round_2=score_dvd1_1round
replace score_dvd1_1round_2=0 if score_dvd1_1round==score1

gen score_exd_1round_2=score_exd_1round
replace score_exd_1round_2=0 if score_exd_1round==score1

gen score_dvd2_1round_2=score_dvd2_1round
replace score_dvd2_1round_2=0 if score_dvd2_1round==score1

gen score_dvd3_1round_2=score_dvd3_1round
replace score_dvd3_1round_2=0 if score_dvd3_1round==score1

gen score_prv_1round_2=score_prv_1round
replace score_prv_1round_2=0 if score_prv_1round==score1

gen score_nce_1round_2=score_nce_1round
replace score_nce_1round_2=0 if score_nce_1round==score1

gen score_eco1_1round_2=score_eco1_1round
replace score_eco1_1round_2=0 if score_eco1_1round==score1

gen score_aut2_1round_2=score_aut2_1round
replace score_aut2_1round_2=0 if score_aut2_1round==score1

gen score_exg3_1round_2=score_exg3_1round
replace score_exg3_1round_2=0 if score_exg3_1round==score1

gen score_alli_1round_2=score_alli_1round
replace score_alli_1round_2=0 if score_alli_1round==score1

gen score_rdg_1round_2=score_rdg_1round
replace score_rdg_1round_2=0 if score_rdg_1round==score1

gen score_eco2_1round_2=score_eco2_1round
replace score_eco2_1round_2=0 if score_eco2_1round==score1

gen score_dvd4_1round_2=score_dvd4_1round
replace score_dvd4_1round_2=0 if score_dvd4_1round==score1

gen score_aut3_1round_2=score_aut3_1round
replace score_aut3_1round_2=0 if score_aut3_1round==score1

gen score_aut4_1round_2=score_aut4_1round
replace score_aut4_1round_2=0 if score_aut4_1round==score1

gen score_dvg1_1round_2=score_dvg1_1round
replace score_dvg1_1round_2=0 if score_dvg1_1round==score1

gen score_dvg2_1round_2=score_dvg2_1round
replace score_dvg2_1round_2=0 if score_dvg2_1round==score1

gen score_dvd5_1round_2=score_dvd5_1round
replace score_dvd5_1round_2=0 if score_dvd5_1round==score1

gen score_dvg3_1round_2=score_dvg3_1round
replace score_dvg3_1round_2=0 if score_dvg3_1round==score1

gen score_reg_1round_2=score_reg_1round
replace score_reg_1round_2=0 if score_reg_1round==score1

gen score_exd1_1round_2=score_exd1_1round
replace score_exd1_1round_2=0 if score_exd1_1round==score1

gen score2=max(score_ump_1round_2, score_aut_1round_2, score_dvd_1round_2, score_cen_1round_2, score_exg_1round_2, score_aut1_1round_2, score_eco_1round_2, score_exg1_1round_2, score_soc_1round_2, score_vec_1round_2, score_dvg_1round_2, score_fg_1round_2, score_exg2_1round_2, score_fn_1round_2, score_dvd1_1round_2, score_exd_1round_2, score_dvd2_1round_2, score_dvd3_1round_2, score_prv_1round_2, score_nce_1round_2, score_eco1_1round_2, score_aut2_1round_2, score_exg3_1round_2, score_alli_1round_2, score_rdg_1round_2, score_eco2_1round_2, score_dvd4_1round_2, score_aut3_1round_2, score_aut4_1round_2, score_dvg1_1round_2, score_dvg2_1round_2, score_dvd5_1round_2, score_dvg3_1round_2, score_reg_1round_2, score_exd1_1round_2) if nbr_cand_running>1

gen score_ump_1round_3=score_ump_1round_2
replace score_ump_1round_3=0 if score_ump_1round==score2

gen score_aut_1round_3=score_aut_1round_2
replace score_aut_1round_3=0 if score_aut_1round==score2

gen score_dvd_1round_3=score_dvd_1round_2
replace score_dvd_1round_3=0 if score_dvd_1round==score2

gen score_cen_1round_3=score_cen_1round_2
replace score_cen_1round_3=0 if score_cen_1round==score2

gen score_exg_1round_3=score_exg_1round_2
replace score_exg_1round_3=0 if score_exg_1round==score2

gen score_aut1_1round_3=score_aut1_1round_2
replace score_aut1_1round_3=0 if score_aut1_1round==score2

gen score_eco_1round_3=score_eco_1round_2
replace score_eco_1round_3=0 if score_eco_1round==score2

gen score_exg1_1round_3=score_exg1_1round_2
replace score_exg1_1round_3=0 if score_exg1_1round==score2

gen score_soc_1round_3=score_soc_1round_2
replace score_soc_1round_3=0 if score_soc_1round==score2

gen score_vec_1round_3=score_vec_1round_2
replace score_vec_1round_3=0 if score_vec_1round==score2

gen score_dvg_1round_3=score_dvg_1round_2
replace score_dvg_1round_3=0 if score_dvg_1round==score2

gen score_fg_1round_3=score_fg_1round_2
replace score_fg_1round_3=0 if score_fg_1round==score2

gen score_exg2_1round_3=score_exg2_1round_2
replace score_exg2_1round_3=0 if score_exg2_1round==score2

gen score_fn_1round_3=score_fn_1round_2
replace score_fn_1round_3=0 if score_fn_1round==score2

gen score_dvd1_1round_3=score_dvd1_1round_2
replace score_dvd1_1round_3=0 if score_dvd1_1round==score2

gen score_exd_1round_3=score_exd_1round_2
replace score_exd_1round_3=0 if score_exd_1round==score2

gen score_dvd2_1round_3=score_dvd2_1round_2
replace score_dvd2_1round_3=0 if score_dvd2_1round==score2

gen score_dvd3_1round_3=score_dvd3_1round_2
replace score_dvd3_1round_3=0 if score_dvd3_1round==score2

gen score_prv_1round_3=score_prv_1round_2
replace score_prv_1round_3=0 if score_prv_1round==score2

gen score_nce_1round_3=score_nce_1round_2
replace score_nce_1round_3=0 if score_nce_1round==score2

gen score_eco1_1round_3=score_eco1_1round_2
replace score_eco1_1round_3=0 if score_eco1_1round==score2

gen score_aut2_1round_3=score_aut2_1round_2
replace score_aut2_1round_3=0 if score_aut2_1round==score2

gen score_exg3_1round_3=score_exg3_1round_2
replace score_exg3_1round_3=0 if score_exg3_1round==score2

gen score_alli_1round_3=score_alli_1round_2
replace score_alli_1round_3=0 if score_alli_1round==score2

gen score_rdg_1round_3=score_rdg_1round_2
replace score_rdg_1round_3=0 if score_rdg_1round==score2

gen score_eco2_1round_3=score_eco2_1round_2
replace score_eco2_1round_3=0 if score_eco2_1round==score2

gen score_dvd4_1round_3=score_dvd4_1round_2
replace score_dvd4_1round_3=0 if score_dvd4_1round==score2

gen score_aut3_1round_3=score_aut3_1round_2
replace score_aut3_1round_3=0 if score_aut3_1round==score2

gen score_aut4_1round_3=score_aut4_1round_2
replace score_aut4_1round_3=0 if score_aut4_1round==score2

gen score_dvg1_1round_3=score_dvg1_1round_2
replace score_dvg1_1round_3=0 if score_dvg1_1round==score2

gen score_dvg2_1round_3=score_dvg2_1round_2
replace score_dvg2_1round_3=0 if score_dvg2_1round==score2

gen score_dvd5_1round_3=score_dvd5_1round_2
replace score_dvd5_1round_3=0 if score_dvd5_1round==score2

gen score_dvg3_1round_3=score_dvg3_1round_2
replace score_dvg3_1round_3=0 if score_dvg3_1round==score2

gen score_reg_1round_3=score_reg_1round_2
replace score_reg_1round_3=0 if score_reg_1round==score2

gen score_exd1_1round_3=score_exd1_1round_2
replace score_exd1_1round_3=0 if score_exd1_1round==score2

gen score3=max(score_ump_1round_3, score_aut_1round_3, score_dvd_1round_3, score_cen_1round_3, score_exg_1round_3, score_aut1_1round_3, score_eco_1round_3, score_exg1_1round_3, score_soc_1round_3, score_vec_1round_3, score_dvg_1round_3, score_fg_1round_3, score_exg2_1round_3, score_fn_1round_3, score_dvd1_1round_3, score_exd_1round_3, score_dvd2_1round_3, score_dvd3_1round_3, score_prv_1round_3, score_nce_1round_3, score_eco1_1round_3, score_aut2_1round_3, score_exg3_1round_3, score_alli_1round_3, score_rdg_1round_3, score_eco2_1round_3, score_dvd4_1round_3, score_aut3_1round_3, score_aut4_1round_3, score_dvg1_1round_3, score_dvg2_1round_3, score_dvd5_1round_3, score_dvg3_1round_3, score_reg_1round_3, score_exd1_1round_3) if nbr_cand_running>1

* Identying the parties of the three first scores
*************************************************

gen party1=.
replace party1=1 if score1==score_soc_1round
replace party1=2 if score1==score_ump_1round
replace party1=3 if score1==score_fn_1round
replace party1=4 if score1==score_fg_1round
replace party1=5 if score1==score_cen_1round
replace party1=6 if score1==score_vec_1round

gen party2=.
replace party2=1 if score2==score_soc_1round
replace party2=2 if score2==score_ump_1round
replace party2=3 if score2==score_fn_1round
replace party2=4 if score2==score_fg_1round
replace party2=5 if score2==score_cen_1round
replace party2=6 if score2==score_vec_1round

gen party3=.
replace party3=1 if score3==score_soc_1round
replace party3=2 if score3==score_ump_1round
replace party3=3 if score3==score_fn_1round
replace party3=4 if score3==score_fg_1round
replace party3=5 if score3==score_cen_1round
replace party3=6 if score3==score_vec_1round

gen party1_bis=.
replace party1_bis=1 if score1==score_soc_1round
replace party1_bis=2 if score1==score_ump_1round
replace party1_bis=3 if score1==score_fn_1round
replace party1_bis=4 if score1==score_fg_1round
replace party1_bis=5 if score1==score_cen_1round
replace party1_bis=6 if score1==score_vec_1round

replace party1_bis=1 if score1==score_dvg_1round
replace party1_bis=1 if score1==score_dvg1_1round
replace party1_bis=1 if score1==score_dvg2_1round
replace party1_bis=1 if score1==score_dvg3_1round
replace party1_bis=2 if score1==score_dvd_1round
replace party1_bis=2 if score1==score_dvd1_1round
replace party1_bis=2 if score1==score_dvd2_1round
replace party1_bis=2 if score1==score_dvd3_1round
replace party1_bis=3 if score1==score_exd_1round
replace party1_bis=3 if score1==score_exd1_1round
replace party1_bis=4 if score1==score_exg_1round
replace party1_bis=4 if score1==score_exg1_1round
replace party1_bis=4 if score1==score_exg2_1round
replace party1_bis=4 if score1==score_exg3_1round

gen party2_bis=.
replace party2_bis=1 if score2==score_soc_1round
replace party2_bis=2 if score2==score_ump_1round
replace party2_bis=3 if score2==score_fn_1round
replace party2_bis=4 if score2==score_fg_1round
replace party2_bis=5 if score2==score_cen_1round
replace party2_bis=6 if score2==score_vec_1round

replace party2_bis=1 if score2==score_dvg_1round
replace party2_bis=1 if score2==score_dvg1_1round
replace party2_bis=1 if score2==score_dvg2_1round
replace party2_bis=1 if score2==score_dvg3_1round
replace party2_bis=2 if score2==score_dvd_1round
replace party2_bis=2 if score2==score_dvd1_1round
replace party2_bis=2 if score2==score_dvd2_1round
replace party2_bis=2 if score2==score_dvd3_1round
replace party2_bis=3 if score2==score_exd_1round
replace party2_bis=3 if score2==score_exd1_1round
replace party2_bis=4 if score2==score_exg_1round
replace party2_bis=4 if score2==score_exg1_1round
replace party2_bis=4 if score2==score_exg2_1round
replace party2_bis=4 if score2==score_exg3_1round

gen party3_bis=.
replace party3_bis=1 if score3==score_soc_1round
replace party3_bis=2 if score3==score_ump_1round
replace party3_bis=3 if score3==score_fn_1round
replace party3_bis=4 if score3==score_fg_1round
replace party3_bis=5 if score3==score_cen_1round
replace party3_bis=6 if score3==score_vec_1round

replace party3_bis=1 if score3==score_dvg_1round
replace party3_bis=1 if score3==score_dvg1_1round
replace party3_bis=1 if score3==score_dvg2_1round
replace party3_bis=1 if score3==score_dvg3_1round
replace party3_bis=2 if score3==score_dvd_1round
replace party3_bis=2 if score3==score_dvd1_1round
replace party3_bis=2 if score3==score_dvd2_1round
replace party3_bis=2 if score3==score_dvd3_1round
replace party3_bis=3 if score3==score_exd_1round
replace party3_bis=3 if score3==score_exd1_1round
replace party3_bis=4 if score3==score_exg_1round
replace party3_bis=4 if score3==score_exg1_1round
replace party3_bis=4 if score3==score_exg2_1round
replace party3_bis=4 if score3==score_exg3_1round

* Calculating ideological distance between respondents and each party
*********************************************************************

replace Q30A=. if Q30A==99
replace Q30AA=. if Q30AA==99
replace Q30AB=. if Q30AB==99
replace Q30AC=. if Q30AC==99
replace Q30AD=. if Q30AD==99
replace Q30AE=. if Q30AE==99
replace Q30AF=. if Q30AF==99

sum Q30AA
sum Q30AB
sum Q30AC
sum Q30AD
sum Q30AE
sum Q30AF

gen position_soc=3.598913
gen position_ump=7.214129
gen position_fn=9.031642
gen position_fdg=1.615124
gen position_modem=4.95059
gen position_vec=3.095906

gen distance_soc=abs(Q30A-position_soc)
gen distance_ump=abs(Q30A-position_ump)
gen distance_fn=abs(Q30A-position_fn)
gen distance_fdg=abs(Q30A-position_fdg)
gen distance_modem=abs(Q30A-position_modem)
gen distance_vec=abs(Q30A-position_vec)

gen distance_party1=.
replace distance_party1=distance_soc if party1==1
replace distance_party1=distance_ump if party1==2
replace distance_party1=distance_fn if party1==3
replace distance_party1=distance_fdg if party1==4
replace distance_party1=distance_modem if party1==5
replace distance_party1=distance_vec if party1==6

gen distance_party2=.
replace distance_party2=distance_soc if party2==1
replace distance_party2=distance_ump if party2==2
replace distance_party2=distance_fn if party2==3
replace distance_party2=distance_fdg if party2==4
replace distance_party2=distance_modem if party2==5
replace distance_party2=distance_vec if party2==6

gen distance_party3=.
replace distance_party3=distance_soc if party3==1
replace distance_party3=distance_ump if party3==2
replace distance_party3=distance_fn if party3==3
replace distance_party3=distance_fdg if party3==4
replace distance_party3=distance_modem if party3==5
replace distance_party3=distance_vec if party3==6


* Calculating differences between distances to the third party compared to the closest of the two first parties
***************************************************************************************************************

gen distance_12=min(distance_party1, distance_party2)
gen proximity_12=10-distance_12

gen proximity_3=10-distance_party3

gen proximity_diff=proximity_3-proximity_12
replace proximity_diff=. if distance_party3==.
replace proximity_diff=. if distance_12==.


* Creating variable capturing the closeness of the results (1st and 2nd round)
******************************************************************************

gen close1=score1-score2

gen score1_2R=max(score_ump_2round, score_soc_2round, score_vec_2round, score_dvd_2round, score_prv_2round, score_fg_2round, score_dvd1_2round, score_nce_2round, score_fn_2round, score_rdg_2round, score_dvg_2round, score_exd_2round)

gen score_ump_2round_2=score_ump_2round
replace score_ump_2round_2=0 if score_ump_2round==score1_2R

gen score_soc_2round_2=score_soc_2round
replace score_soc_2round_2=0 if score_soc_2round==score1_2R

gen score_vec_2round_2=score_vec_2round
replace score_vec_2round_2=0 if score_vec_2round==score1_2R

gen score_dvd_2round_2=score_dvd_2round
replace score_dvd_2round_2=0 if score_dvd_2round==score1_2R

gen score_prv_2round_2=score_prv_2round
replace score_prv_2round_2=0 if score_prv_2round==score1_2R

gen score_fg_2round_2=score_fg_2round
replace score_fg_2round_2=0 if score_fg_2round==score1_2R

gen score_dvd1_2round_2=score_dvd1_2round
replace score_dvd1_2round_2=0 if score_dvd1_2round==score1_2R

gen score_nce_2round_2=score_nce_2round
replace score_nce_2round_2=0 if score_nce_2round==score1_2R

gen score_fn_2round_2=score_fn_2round
replace score_fn_2round_2=0 if score_fn_2round==score1_2R

gen score_rdg_2round_2=score_rdg_2round
replace score_rdg_2round_2=0 if score_rdg_2round==score1_2R

gen score_dvg_2round_2=score_dvg_2round
replace score_dvg_2round_2=0 if score_dvg_2round==score1_2R

gen score_exd_2round_2=score_exd_2round
replace score_exd_2round_2=0 if score_exd_2round==score1_2R

gen score2_2R=max(score_ump_2round_2, score_soc_2round_2, score_vec_2round_2, score_dvd_2round_2, score_prv_2round_2, score_fg_2round_2, score_dvd1_2round_2, score_nce_2round_2, score_fn_2round_2, score_rdg_2round_2, score_dvg_2round_2, score_exd_2round_2)

gen close2=score1_2R-score2_2R

* Creating control variables
****************************

gen duty=.
replace duty=1 if Q44B==1
replace duty=0 if Q44B==2

gen pol_interest=.
replace pol_interest=Q5
replace pol_interest=. if Q5==99

gen party_id=.
replace party_id=1 if Q46==1
replace party_id=0 if Q46==2

gen importance=.
replace importance=Q34A
replace importance=. if Q34A==99


* Identifying number of districts

bysort ELEC_CON: gen district_id=1 if _n==1
tab nbr_cand_running if nbr_cand_running>1 & district_id==1


* Descriptive statistics
*************************

tab nbr_cand_running if district_id==1
tab nbr_cand_running if nbr_cand_running>1 & district_id==1
tab nbr_cand_running if ELEC_CON!=.

tab turnout REGION, col

gen parties=1
replace parties=0 if party1==.
replace parties=0 if party2==.
replace parties=0 if party3==.

tab parties if ELEC_CON!=., mi
tab parties if ELEC_CON!=. & district_id==1, mi


* Regressions
**************

* Main effect

reg turnout nbr_cand_running if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & party_id!=. & importance!=. & close2!=. & REGION!=., vce(cluster ELEC_CON)
reg turnout nbr_cand_running AGE GEND EDUC income i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & party_id!=. & importance!=.  & close2!=. & REGION!=., vce(cluster ELEC_CON)
reg turnout nbr_cand_running AGE GEND EDUC income duty pol_interest party_id importance close2 i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & party_id!=. & importance!=.  & close2!=. & REGION!=., vce(cluster ELEC_CON)


* Mechanism

* Table

reg turnout i.nbr_cand_running##c.proximity_diff if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & party_id!=. & importance!=. & close2!=. & REGION!=. &proximity_diff<=0, vce(cluster ELEC_CON)

reg turnout i.nbr_cand_running##c.proximity_diff if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & party_id!=. & importance!=. & close2!=. & REGION!=. &proximity_diff>0, vce(cluster ELEC_CON)

reg turnout i.nbr_cand_running##c.proximity_diff AGE GEND EDUC income i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & close2!=.  & party_id!=. & importance!=. & REGION!=. &proximity_diff<=0, vce(cluster ELEC_CON)

reg turnout i.nbr_cand_running##c.proximity_diff AGE GEND EDUC income i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & close2!=. & party_id!=. & importance!=. & REGION!=. &proximity_diff>0, vce(cluster ELEC_CON)

reg turnout i.nbr_cand_running##c.proximity_diff AGE GEND EDUC income duty pol_interest party_id importance close2 i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & close2!=. & party_id!=. & importance!=. & REGION!=. &proximity_diff<=0, vce(cluster ELEC_CON)

reg turnout i.nbr_cand_running##c.proximity_diff AGE GEND EDUC income duty pol_interest party_id importance close2 i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & close2!=. & party_id!=. & importance!=. & REGION!=. &proximity_diff>0, vce(cluster ELEC_CON)

* Graph

reg turnout i.nbr_cand_running##c.proximity_diff AGE GEND EDUC income duty pol_interest party_id importance close2 i.REGION if nbr_cand_running>1 & AGE!=. & GEND!=. & EDUC!=. & income!=. & duty!=. & pol_interest!=. & party_id!=. & importance!=. & close2!=. & REGION!=. &proximity_diff>0, vce(cluster ELEC_CON)
margins, dydx(nbr_cand_running) at(proximity_diff=(1(1)6)) atmeans 

marginsplot, scheme(s2mono) ylabel(-.1(.1).8) recast(line) recastci(rline) ci(lp(dash)) yline(0,lp(dot)) xtitle("Proximity to third candidate (relative to two first)") ytitle("Effect of 3rd candidate running") title("") graphregion(color(white))  addplot(hist proximity_diff if proximity_diff>=1 & proximity_diff<=6, frac lcolor(gs12) color(none) xlabel(1(1)6) ylabel(-.1(.1).8)) legend(off)

* Extra analysis
****************

* Chekcing whether there are more missing values in turnout depending on number of candidates running

gen turnout_missing=0
replace turnout_missing=1 if turnout==.

ttest turnout_missing if nbr_cand_running>1, by(nbr_cand_running)

